﻿using System.Windows;
using System.Windows.Controls;
using SQLToolbox.View.Types;

namespace SQLToolbox.View.UserControls
{
	
	[TemplatePart(Name = "btnClose", Type = typeof(Button))]
	[TemplatePart(Name = "tabProgress", Type = typeof(CircularProgressBar))]
	public class ClosableTabItem : TabItem, IProgressTab
	{
		CircularProgressBar tabsProgress;
		Visibility progressState = Visibility.Hidden;
		object tooltip = string.Empty;

		static ClosableTabItem()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(ClosableTabItem), new FrameworkPropertyMetadata(typeof(ClosableTabItem)));
		}


		public static readonly RoutedEvent CloseTabEvent =
			EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
				typeof(RoutedEventHandler), typeof(ClosableTabItem));


		public static readonly RoutedEvent ContentChangedEvent =
			EventManager.RegisterRoutedEvent("ContentChanged", RoutingStrategy.Bubble,
				typeof(RoutedEventHandler), typeof(ClosableTabItem));

		public event RoutedEventHandler CloseTab
		{
			add { AddHandler(CloseTabEvent, value); }
			remove { RemoveHandler(CloseTabEvent, value); }
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			Button closeButton = this.Template.FindName("btnClose", this) as Button;
			if (closeButton != null)
				closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
			SetProgressState(progressState);
			SetToolTip(tooltip);
		}

		public new object Content
		{
			get
			{
				return base.Content;
			}
			set
			{
				base.Content = value;
				this.RaiseEvent(new RoutedEventArgs(ContentChangedEvent, this));
			}
		}

		public event RoutedEventHandler ContentChanged
		{
			add { AddHandler(ContentChangedEvent, value); }
			remove { RemoveHandler(ContentChangedEvent, value); }
		}

		void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
		}

		public Visibility ProgressState
		{
			get
			{
				return progressState;
			}
			set
			{
				SetProgressState(value);
			}

		}

		public void StartProgress()
		{
			SetProgressState(Visibility.Visible);
		}


		public void StopProgress()
		{
			SetProgressState(Visibility.Hidden);
		}

		private void SetProgressState(Visibility value)
		{
			progressState = value;
			if (this.Template == null)
				return;
			tabsProgress = this.Template.FindName("tabProgress", this) as CircularProgressBar;
			if (tabsProgress != null)
				tabsProgress.Visibility = progressState;
		}

		public new object ToolTip
		{
			get
			{

				return tooltip;
			}
			set
			{
				SetToolTip(value);

			}
		}

		private void SetToolTip(object value)
		{
			this.tooltip = value;
			if (this.Template == null)
				return;
			var sp = this.Template.FindName("tabHeader", this)as StackPanel;
			if (sp != null)
				sp.ToolTip = value;
		}
	}
}
